package com.prj.ufdm.webflux.api;

import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RestController
@RequestMapping("/flux")
public class FluxTestApi {
	
	private static final Log log = LogFactory.getLog(FluxTestApi.class);
	
	@RequestMapping(value="/test",produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> test() throws Exception {
		log.info("flux test start");
		Flux<String> result = Flux.fromStream(IntStream.range(1,10).mapToObj(i -> {
			try {
				TimeUnit.SECONDS.sleep(2);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			return "flux-data:"+i;
		}));
		log.info("flux end start");
		return result;
    }
	
	@GetMapping("/mvctest")
    public String getMvcTest() throws Exception {
		log.info("mvctest start");
		String result = cretateStr();
		log.info("mvctest end");
		return result;
    }
	
	
	private String cretateStr() {
		try {
			TimeUnit.SECONDS.sleep(2);
		}
		catch(Exception er) {
		}
		return "this is test str";
	}
	
}
